﻿@{
    Layout = "~/Views/Shared/_FormWhite.cshtml";
    OperatorInfo operatorInfo = ViewBag.OperatorInfo;
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-2 control-label ">登录名称<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="userName" col="UserName" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">登录密码<font class="red"> *</font></label>
            <div class="col-sm-4">
                <input id="password" col="Password" type="password" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">姓名</label>
            <div class="col-sm-4">
                <input id="realName" col="RealName" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">性别</label>
            <div class="col-sm-4" id="gender" col="Gender"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">生日</label>
            <div class="col-sm-4">
                <input id="birthday" col="Birthday" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">部门</label>
            <div class="col-sm-4">
                <div id="departmentId" col="DepartmentId"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">手机</label>
            <div class="col-sm-4">
                <input id="mobile" col="Mobile" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label ">职位</label>
            <div class="col-sm-4">
                <div id="positionId" col="PositionIds"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">邮箱</label>
            <div class="col-sm-4">
                <input id="email" col="Email" type="text" class="form-control" />
            </div>
            <label class="col-sm-2 control-label">状态</label>
            <div class="col-sm-4" id="userStatus" col="UserStatus"></div>
        </div>
        <div class="form-group" style="display:none;">
            <label id="rolelab" class="col-sm-2 control-label">角色</label>
            <div class="col-sm-4" id="role" col="RoleIds"></div>
            <label id="IsSystemlab" class="col-sm-2 control-label">账号类型</label>
            <div class="col-sm-4" id="IsSystem" col="IsSystem"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">绑定微信</label>
            <div class="col-sm-4" id="openid" col="openid"></div>
            <label id="tenant_lab" class="col-sm-2 control-label">客户</label>
            <div class="col-sm-4" id="tenant_id" col="tenant_id"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">备注</label>
            <div class="col-sm-10">
                <textarea id="remark" class="form-control" style="height:60px"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    $(function () {
        $("#userStatus").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(StatusEnum).EnumToDictionaryString())) });
        $("#gender").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(YiSha.Enum.OrganizationManage.GenderTypeEnum).EnumToDictionaryString())) });

        $("#tenant_id").ysComboBox({
            url: '@Url.Content("~/OrganizationManage/SysTenant/GetListJson")',
            key: "Id",
            value: "name",
            class: "form-control",
        });

        $("#openid").ysComboBox({
            url: '@Url.Content("~/QixingManage/CustomerUser/GetListJson")',
            key: "openid",
            value: "nickname",
            class: "form-control",
        });

        $("#positionId").ysComboBox({
            url: '@Url.Content("~/OrganizationManage/Position/GetListJson")',
            key: "Id",
            value: "PositionName",
            class: "form-control",
            multiple: true
        });

         $("#role").ysCheckBox({
            url: '@Url.Content("~/SystemManage/Role/GetListJson")',
            key: "Id",
            value: "RoleName"
         });



        if (@operatorInfo.IsSystem== 1) {
            $("#role").parent().show();

            $("#IsSystem").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(IsSystemEnum).EnumToDictionaryString())) });
        }
        else {
            //$("#IsSystem").text("客户账号");
            //$("#role").parent().hide();
            roleshow();
        }

        $("#IsSystem input[type='radio']").on('ifClicked', function (event) {
            var value = $(this).val();
            if (value == 0) {
                if (@operatorInfo.IsSystem!=1)
                {
                    $("#tenant_id_select").select2({
                        "disabled": true
                    });
                }

                $("#tenant_lab").show();
                $("#tenant_id").show();
            }
            else {
                $("#tenant_id_select").select2({
                    "disabled": false
                });
                $("#tenant_lab").hide();
                $("#tenant_id").hide();
            }
        });

        $('#departmentId').ysComboBoxTree({ url: '@Url.Content("~/OrganizationManage/Department/GetDepartmentTreeListJson")' });

        laydate.render({ elem: '#birthday', format: 'yyyy-MM-dd' });

        getForm(id);

        $("#form").validate({
            rules: {
                userName: { required: true },
                password: {
                    required: true,
                    minlength: 6,
                    maxlength: 20
                },
                mobile: { isPhone: true},
                email: { email: true}
            }
        });
    });

    function roleshow()
    {
        ys.ajax({
                url: '@Url.Content("~/SystemManage/Role/GetListJson")',
                type: "get",
                success: function (obj) {
                    if (obj.Tag == 1) {
                        var result = obj.Data;
                        if (result.length > 0) {
                            $("#role").parent().show();
                            $("#role").show();
                            $("#rolelab").show();
                            $("#IsSystemlab").hide();
                            $("#IsSystem").hide();
                        }
                        else
                        {
                            $("#role").parent().hide();
                        }
                    }
                }
            });
    }

    function getForm() {
        if (id > 0) {
            $('#password').attr("readonly", "readonly").attr("disabled", "disabled");

            ys.ajax({
                url: '@Url.Content("~/OrganizationManage/User/GetFormJson")' + '?id=' + id,
                type: "get",
                success: function (obj) {
                    if (obj.Tag == 1) {
                        var result = obj.Data;
                        $("#form").setWebControls(result);
                        if (result.tenant_id != null && result.tenant_id!=0) {
                            //禁用掉客户账号权限
                            $("#tenant_id_select").select2({
                                "disabled": true
                            });
                        }
                    }
                }
            });
        }
        else {
            var defaultData = {};
            defaultData.UserName = "";
            defaultData.Password = ""
            defaultData.UserStatus = "@StatusEnum.Yes.ParseToInt()";
            defaultData.IsSystem = "@IsSystemEnum.客户账号.ParseToInt()";
            $("#form").setWebControls(defaultData);
        }
    }

    function saveForm(index) {
        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            postData.DepartmentId = ys.getLastValue(postData.DepartmentId);
            if (@operatorInfo.IsSystem!=1)
            {
                postData.IsSystem = @operatorInfo.IsSystem;
            }

            if (postData.IsSystem == 0) {
                if (postData.tenant_id == null) {
                    return ys.msgError("请选择客户!");
                }
            }
            else
            {
                postData.tenant_id= 0;
            }

            postData.WeChat = $('#openid').ysComboBox('getText');
            ys.ajax({
                url: '@Url.Content("~/OrganizationManage/User/SaveFormJson")',
                type: "post",
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }
</script>
